package com.guoq.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoq.mapper.CartMapper;
import com.guoq.pojo.Cart;
import com.guoq.pojo.CartExample;
import com.guoq.pojo.CartExample.Criteria;
import com.guoq.pojo.EUDataGridResult;
import com.guoq.pojo.Result;
import com.guoq.service.ICartService;
import com.guoq.utils.IDUtils;

@Service("cartService")
public class CartServiceImpl implements ICartService {

	@Resource
	private CartMapper cartMapper;

	@Override
	public Cart getById(int id) {
		// TODO Auto-generated method stub
		return cartMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Cart> getList() {
		
		// TODO Auto-generated method stub
		CartExample example = new CartExample();
		example.setOrderByClause("id");
		List<Cart> list = cartMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public EUDataGridResult getPageList(Cart record, Integer page, Integer rows) {
		
		// TODO Auto-generated method stub
		CartExample example = new CartExample();
		Criteria criteria = example.createCriteria();
//		String name = record.getName();
//		if(Strkit.notBlank(name)){
//			criteria.andNameLike(name);
//		}
		
		example.setOrderByClause("createtime desc");
		
		//分页处理
		PageHelper.startPage(page, rows);
		List<Cart> list = cartMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Cart> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public List<Cart> selectByExample(CartExample example) {
		// TODO Auto-generated method stub
		List<Cart> list = cartMapper.selectByExample(example);
		return list;
	}

	@Override
	public Cart selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		Cart record = cartMapper.selectByPrimaryKey(id);
		return record;
	}

	@Override
	public int countByExample(CartExample example) {
		// TODO Auto-generated method stub
		int count = cartMapper.countByExample(example);
		return count;
	}

	@Override
	public Result create(Cart record) throws Exception {

		String shopid = IDUtils.genShopId();
		
		Result result = this.insert(shopid, record);
		if(result.getStatus() != 200){
			throw new Exception();
		}
		return Result.ok();
	}
	
	/**
	 * 添加商品描述
	 *<P>开发者:郭强
	 *<P>方法名: insertItemDesc
	 *<P>描述: 
	 *@param itemId
	 *@param desc
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	private Result insert(String shopId, Cart record){
		
		record.setCreatetime(new Date());
		record.setUpdatetime(new Date());
		cartMapper.insertSelective(record);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public Result delete(Integer id) throws Exception {

		
		Result result = null;
		
		int num = cartMapper.deleteByPrimaryKey(id);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

	/**
	 * 编辑
	 */
	@Override
	public Result update(Cart record) throws Exception {

		Result result = null;
		int num = cartMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}
}
